<?php
/**
 * Include and setup custom metaboxes and fields.
 *
 * @category YourThemeOrPlugin
 * @package  Metaboxes
 * @license  http://www.opensource.org/licenses/gpl-license.php GPL v2.0 (or later)
 * @link     https://github.com/jaredatch/Custom-Metaboxes-and-Fields-for-WordPress
 */

add_filter( 'cmb_meta_boxes', 'cmb_portfolio_metaboxes' );
/**
 * Define the metabox and field configurations.
 *
 * @param  array $meta_boxes
 * @return array
 */
function cmb_portfolio_metaboxes( array $meta_boxes ) {

	// Start with an underscore to hide fields from custom fields list
	$prefix = '_portfolio_';
	
	$categories = array();  
	$categories_obj = get_categories('hide_empty=0');
	foreach ($categories_obj as $cat) {
		$categories[$cat->cat_ID] = $cat->cat_name;
	}

	
	$meta_boxes[] = array(
		'id'         => 'portfolio_page_metabox',
		'title'      => 'Portfolio settings',
		'pages'      => array( 'page'), // Post type
		'context'    => 'normal',
		'priority'   => 'high',
		'show_names' => true, // Show field names on the left
		'fields'     => array(			
			array(
				'name'    => 'Items amount per page',
				'desc'    => 'Amount of portfolio items per page.',
				'id'      => $prefix . 'items_amount',
				'type'    => 'select',
				'options' => array(
					array( 'name' => '1', 'value' => '1', ),
					array( 'name' => '2', 'value' => '2', ),
					array( 'name' => '3', 'value' => '3', ),
					array( 'name' => '4', 'value' => '4', ),
					array( 'name' => '5', 'value' => '5', ),
					array( 'name' => '6', 'value' => '6', ),
					array( 'name' => '7', 'value' => '7', ),
					array( 'name' => '8', 'value' => '8', ),
					array( 'name' => '9', 'value' => '9', ),
					array( 'name' => '10', 'value' => '10', ),
					array( 'name' => '11', 'value' => '11', ),
					array( 'name' => '12', 'value' => '12', ),
					array( 'name' => '13', 'value' => '13', ),
					array( 'name' => '14', 'value' => '14', ),
					array( 'name' => '15', 'value' => '15', ),
					array( 'name' => '16', 'value' => '16', ),
					array( 'name' => '17', 'value' => '17', ),
					array( 'name' => '18', 'value' => '18', ),
					array( 'name' => '19', 'value' => '19', ),
					array( 'name' => '20', 'value' => '20', ),
					array( 'name' => '21', 'value' => '21', ),
					array( 'name' => '22', 'value' => '22', ),
					array( 'name' => '23', 'value' => '23', ),
					array( 'name' => '24', 'value' => '24', ),
					array( 'name' => '25', 'value' => '25', ),
					array( 'name' => '26', 'value' => '26', ),
					array( 'name' => '27', 'value' => '27', ),
					array( 'name' => '28', 'value' => '28', ),
					array( 'name' => '29', 'value' => '29', ),
					array( 'name' => '30', 'value' => '30', ),					
				),
			),			
			array(
				'name'    => 'Portfolio layout',
				'desc'    => 'Select portfolio layout style.',
				'id'      => $prefix . 'layout_select',
				'type'    => 'select',
				'options' => array(
					array( 'name' => '4 column', 'value' => 'four-column', ),
					array( 'name' => '3 column', 'value' => 'three-column', ),
					array( 'name' => '2 column', 'value' => 'two-column', ),					
				),
			),
			
			array(
				'name'     => 'Portfolio categories',
				'desc'     => 'Portfolio categories',
				'id'       => $prefix . 'category_multicheck',
				'type'     => 'multicheck',
				'options' => $categories, // Taxonomy Slug
			),
		
		),
	);
	
	$meta_boxes[] = array(
		'id'         => 'portfolio_post_metabox',
		'title'      => 'Post type',
		'pages'      => array( 'post'), // Post type
		'context'    => 'side',
		'priority'   => 'high',
		'show_names' => true, // Show field names on the left
		'fields'     => array(		
			array(
				'name'    => 'Post type',
				'desc'    => '',
				'id'      => $prefix . 'posttype_select',
				'type'    => 'select',
				'options' => array(
					array( 'name' => 'Post (default)', 'value' => 'post', ),
					array( 'name' => 'Video', 'value' => 'video', ),	
					array( 'name' => 'Gallery', 'value' => 'gallery', ),	
				),
			),
		),
	);
	
	$meta_boxes[] = array(
		'id'         => 'portfolio_post_video_metabox',
		'title'      => 'Video link',
		'pages'      => array( 'post'), // Post type
		'context'    => 'normal',
		'priority'   => 'high',
		'show_names' => true, // Show field names on the left
		'fields'     => array(			
			array(
				'name' => 'Video link',
				'desc' => '',
				'id'   => $prefix . 'videolink_text',
				'type' => 'textarea',
			),			
		),
	);
	
	return $meta_boxes;
}

add_action('admin_enqueue_scripts', 'portfolio_metabox_script');
function portfolio_metabox_script()
{
    wp_enqueue_script('metabox-script', trailingslashit(CVINCLUDES_PORTFOLIO_URI) . 'scripts/metabox.jquery.js', array('jquery'));
}

add_action( 'init', 'portfolio_initialize_cmb_meta_boxes', 9999 );
/**
 * Initialize the metabox class.
 */
function portfolio_initialize_cmb_meta_boxes() {

	if ( ! class_exists( 'cmb_Meta_Box' ) )
		require_once trailingslashit( CVINCLUDES_DIR ) . 'custom-metabox/init.php';

}